package com.searchit.model;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.searchit.R;

public class FileTreeData extends BaseAdapter{

	static class ViewHolder {
		private ImageView image;
		private TextView source;
	}
	
	private LayoutInflater mInflater;
	private List<String> paths;	
	
	public FileTreeData(Context context, List<String> paths){
		
		mInflater = LayoutInflater.from(context);
		this.paths = paths;
	}

	@Override
	public int getCount() {
		
		return paths.size();
	}

	@Override
	public String getItem(int position) {
		 
		return paths.get(position);
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder;
		
		if (convertView == null) {			
			convertView = mInflater.inflate(R.layout.row, null);
			
			holder = new ViewHolder();
			
			holder.image = (ImageView)convertView.findViewById(R.id.image);
			holder.source = (TextView)convertView.findViewById(R.id.rowtext);
			
			convertView.setTag(holder);			
		} else {
			
			holder = (ViewHolder) convertView.getTag();
		}
		
		if(paths.get(position).endsWith("/"))
			holder.image.setImageResource(R.drawable.folder_icon);
		else
			holder.image.setImageResource(R.drawable.file);
		holder.source.setText(paths.get(position));
		
		return convertView;
	}
}